/**
 * 
 */
package com.nh.actionPhone;

import java.sql.Timestamp;
import java.util.List;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.nh.common.DictionaryKey;
import com.nh.entity.NgInteractiveRecovery;
import com.nh.entity.NgInteractiveTopic;

/**
 * 项目名称：NotoginsengManage
 * 
 * 类名称：InteractiveCallAction
 * 
 * 类描述：
 * 
 * 创建人：李兴建
 * 
 * E-mail:lxj08193@163.com
 * 
 * 创建时间：2012-11-14 下午02:50:21
 * 
 * 修改人：李兴建
 * 
 * 修改时间：2012-11-14 下午02:50:21
 * 
 * 修改备注：
 * 
 * @version
 */
public class InteractiveCallAction extends BasePhoneAction {
	private String title;
	private String content;
	private int contentType;
	private int startNum = 0;
	private int limit = 0;
	private Long optionId;
	private NgInteractiveTopic interactiveTopic;
	private List<NgInteractiveRecovery> interactiveRecoveryList;

	/**
	 * 发布话题
	 */
	public void issueInteractive() {
		NgInteractiveTopic interactiveTopic = new NgInteractiveTopic();
		interactiveTopic.setCreateTime(new Timestamp(System.currentTimeMillis()));
		interactiveTopic.setAdmintag(0);
		interactiveTopic.setUserCode(this.getCurrUser().getId());
		interactiveTopic.setRevoverCount(0);
		interactiveTopic.setVisitViewCount(0);
		interactiveTopic.setType(contentType);
		interactiveTopic.setTitle(title);
		interactiveTopic.setContent(content);
		
		int setType=Integer.parseInt(this.getDictionaryValue(DictionaryKey.DICTIONARY_KEY_KEYWORD_SETTYPE));
		if(setType==1){
			writResponse("{\"state\":\"fila\"}");
		}else if(setType==0){
			interactiveTopic.setVerification(2);
		}else if(setType==2){
			String keyWord=this.getDictionaryValue(DictionaryKey.DICTIONARY_KEY_KEYWORD);
			if(keyWord.contains(content)){
				
			}
			
		}
		
		// 根据网站审核确定
		int tag=Integer.parseInt(this.getDictionaryValue(DictionaryKey.DICTIONARY_KEY_VERIFICATION));
		interactiveTopic.setVerification(tag);
		
		this.getBaseService().save(interactiveTopic);
		writResponse("{\"state\":\"success\"}");
	}

	/**
	 * 话题列表
	 */
	public void interactiveList() {
		Criteria criteria = this.createCriteria(NgInteractiveTopic.class);
		criteria.add(Restrictions.eq("verification", 1));
		criteria.addOrder(Order.desc("top"));
		criteria.addOrder(Order.desc("lastReplyTime"));
		criteria.setFirstResult(startNum);
		criteria.setMaxResults(limit);
		List<NgInteractiveTopic> interactiveTopics = criteria.list();
		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "createTime", "publishTime", "verificationTime", "verificationReTime", "lastReplyTime", "remark" });
		writResponse(JSONSerializer.toJSON(interactiveTopics, config).toString());
	}

	/**
	 * 详细信息
	 */
	public String interactiveInfo() {
		interactiveTopic = this.getBaseService().findById(optionId, NgInteractiveTopic.class);
		interactiveRecoveryList = this.getBaseService().findByProperty("recoveryId", interactiveTopic.getId(), NgInteractiveRecovery.class);
		return "interactiveInfo";
	}

	/**
	 * 回复
	 */
	public void repay() {
		interactiveTopic = this.getBaseService().findById(optionId, NgInteractiveTopic.class);
		interactiveTopic.setLastReplyTime(new Timestamp(System.currentTimeMillis()));
		this.getBaseService().saveOrUpdate(interactiveTopic, NgInteractiveTopic.class);
		NgInteractiveRecovery interactiveRecovery=new NgInteractiveRecovery();
		interactiveRecovery.setRecoveryId(interactiveTopic.getId());
		interactiveRecovery.setContent(content);
		interactiveRecovery.setRecoverTime(new Timestamp(System.currentTimeMillis()));
		interactiveRecovery.setUserStr("用户："+this.getCurrUser().getPhoneNum());
		interactiveRecovery.setUserCode(this.getCurrUser().getId()+"");
		this.getBaseService().save(interactiveRecovery);
		writResponse("{\"state\":\"success\"}");
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}

	/**
	 * @param content
	 *            the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * @return the contentType
	 */
	public int getContentType() {
		return contentType;
	}

	/**
	 * @param contentType
	 *            the contentType to set
	 */
	public void setContentType(int contentType) {
		this.contentType = contentType;
	}

	/**
	 * @return the startNum
	 */
	public int getStartNum() {
		return startNum;
	}

	/**
	 * @param startNum
	 *            the startNum to set
	 */
	public void setStartNum(int startNum) {
		this.startNum = startNum;
	}

	/**
	 * @return the limit
	 */
	public int getLimit() {
		return limit;
	}

	/**
	 * @param limit
	 *            the limit to set
	 */
	public void setLimit(int limit) {
		this.limit = limit;
	}

	/**
	 * @return the optionId
	 */
	public Long getOptionId() {
		return optionId;
	}

	/**
	 * @param optionId
	 *            the optionId to set
	 */
	public void setOptionId(Long optionId) {
		this.optionId = optionId;
	}

	/**
	 * @return the interactiveTopic
	 */
	public NgInteractiveTopic getInteractiveTopic() {
		return interactiveTopic;
	}

	/**
	 * @param interactiveTopic
	 *            the interactiveTopic to set
	 */
	public void setInteractiveTopic(NgInteractiveTopic interactiveTopic) {
		this.interactiveTopic = interactiveTopic;
	}

	/**
	 * @return the interactiveRecoveryList
	 */
	public List<NgInteractiveRecovery> getInteractiveRecoveryList() {
		return interactiveRecoveryList;
	}

	/**
	 * @param interactiveRecoveryList
	 *            the interactiveRecoveryList to set
	 */
	public void setInteractiveRecoveryList(List<NgInteractiveRecovery> interactiveRecoveryList) {
		this.interactiveRecoveryList = interactiveRecoveryList;
	}

}
